#include "drawingview.h"
#include "segmentclassifier.h"

#include <QGraphicsScene>
#include <QGraphicsPathItem>
#include <QGraphicsTextItem>
#include <QPen>
#include <QPainterPath>
#include <QWheelEvent>
#include <QResizeEvent>
#include <QDebug>
#include <QPointF>
#include <qmath.h>
#include <cmath>

DrawingView::DrawingView(QWidget *parent)
    : QGraphicsView(parent)
    , m_scene(new QGraphicsScene(this))
{
    setupScene();
    setRenderHint(QPainter::Antialiasing);
    setDragMode(QGraphicsView::ScrollHandDrag);
}

void DrawingView::setupScene()
{
    setScene(m_scene);
    m_scene->setBackgroundBrush(Qt::white);
}

void DrawingView::setPoints(const QList<QPointF> &points)
{
    m_points = points;
    clear();
    drawPoints();
    drawSegments();
    fitToView();
}

void DrawingView::clear()
{
    m_scene->clear();
    m_segments.clear();
    m_labels.clear();
}

void DrawingView::drawPoints()
{
    if (m_points.isEmpty()) return;

    // 绘制点
    for (const QPointF &point : m_points) {
        m_scene->addEllipse(point.x() - 2, point.y() - 2, 4, 4, QPen(Qt::black), QBrush(Qt::black));
    }
#if 0
    // 绘制坐标标签
    for (int i = 0; i < m_points.size(); ++i) {
        QGraphicsTextItem *label = m_scene->addText(QString::number(i + 1));
        label->setPos(m_points[i].x() + 5, m_points[i].y() - 10);
        label->setFont(QFont("Arial", 8));
        m_labels.append(label);
    }
#endif
}

void DrawingView::drawSegments()
{
    if (m_points.size() < 3) {
        // 如果点数少于3个，直接连线
        QPainterPath path;
        for (int i = 0; i < m_points.size(); ++i) {
            if (i == 0) {
                path.moveTo(m_points[i]);
            } else {
                path.lineTo(m_points[i]);
            }
        }
        
        QGraphicsPathItem *item = m_scene->addPath(path);
        item->setPen(QPen(Qt::red, 2));
        m_segments.append(item);
        return;
    }

    // 使用SegmentClassifier判断线段类型
    SegmentClassifier classifier;
    QList<SegmentClassifier::Segment> segments = classifier.classify(m_points);

    for (const SegmentClassifier::Segment &segment : segments) {
        QPainterPath path;
        
        if (segment.type == SegmentClassifier::Line) {
            // 绘制直线
            path.moveTo(segment.start);
            path.lineTo(segment.end);
            
            QGraphicsPathItem *item = m_scene->addPath(path);
            item->setPen(QPen(Qt::red, 2));
            m_segments.append(item);
            
        } else if (segment.type == SegmentClassifier::Arc) {
            // 绘制圆弧
            QPointF center = segment.center;
            qreal radius = segment.radius;
            QRectF rect(center.x() - radius, center.y() - radius, 
                       radius * 2, radius * 2);
            
            // 计算起始和结束角度
            QPointF startVector = segment.start - center;
            QPointF endVector = segment.end - center;
            
            qreal startAngle = qAtan2(startVector.y(), startVector.x()) * 180.0 / M_PI;
            qreal endAngle = qAtan2(endVector.y(), endVector.x()) * 180.0 / M_PI;
            
            // 确保角度正确
            if (segment.clockwise) {
                if (endAngle > startAngle) endAngle -= 360;
            } else {
                if (startAngle > endAngle) startAngle -= 360;
            }
            
            qreal spanAngle = endAngle - startAngle;
            
            path.arcMoveTo(rect, startAngle);
            path.arcTo(rect, startAngle, spanAngle);
            
            QGraphicsPathItem *item = m_scene->addPath(path);
            item->setPen(QPen(Qt::blue, 2));
            m_segments.append(item);
            
            // 绘制圆心
            m_scene->addEllipse(center.x() - 3, center.y() - 3, 6, 6, 
                              QPen(Qt::darkBlue), QBrush(Qt::darkBlue));
        }
    }
}

void DrawingView::fitToView()
{
    if (m_scene->items().isEmpty()) return;
    
    QRectF rect = m_scene->itemsBoundingRect();
    rect.adjust(-50, -50, 50, 50);
    fitInView(rect, Qt::KeepAspectRatio);
}

void DrawingView::resizeEvent(QResizeEvent *event)
{
    QGraphicsView::resizeEvent(event);
    fitToView();
}

void DrawingView::wheelEvent(QWheelEvent *event)
{
    if (event->modifiers() & Qt::ControlModifier) {
        // 缩放
        double scaleFactor = 1.15;
        if (event->angleDelta().y() > 0) {
            scale(scaleFactor, scaleFactor);
        } else {
            scale(1.0 / scaleFactor, 1.0 / scaleFactor);
        }
    } else {
        QGraphicsView::wheelEvent(event);
    }
}
